# -*- coding: utf-8 -*-
# @File  : parse_book.py
# @Author: FanLu
# @Date  : 2021/7/7

# epub文件就是zip文件压缩，可以改名后解压出来，然后读取其中的html文件
# 逻辑来自：https://fanlumaster.github.io/2021/07/08/%E4%BD%BF%E7%94%A8-Python-%E6%8F%90%E5%8F%96-epub-%E4%B8%AD%E7%9A%84%E6%96%87%E6%9C%AC/

from cgitb import text
from contextlib import nullcontext
from bs4 import BeautifulSoup
import re
import os
import pandas as pd

# xhtml_file = open('blossoms/OEBPS/Text/part0009.xhtml', 'r', encoding='utf-8')
# xhtml_handle = xhtml_file.read()
#
# soup = BeautifulSoup(xhtml_handle, 'lxml')
#
# title = soup.find_all("title")
# print(title)
#
# p_list = soup.find_all('p')
# for p in p_list:
#     print(p)


def is_match_class(css):
    return re.compile('(?:calibre_2|calibre_6|bold)').match(css)

def is_match_text(text):
    # return True
    return text is not None and re.compile('(?:[1-9][0-9]?\..*（[A-Z]{2}.*）|【定位】|【主治】|（1）|（2）|（3）|（4）).*').match(text)


def exact_p_tag(path):
    xhtml_file = open(path, 'r', encoding='utf-8')
    xhtml_handle = xhtml_file.read()

    soup = BeautifulSoup(xhtml_handle, 'html.parser')
    # title = soup.find_all("title")
    # print(title)

    p_list = soup.find_all(["p","span"],class_=is_match_class,text=is_match_text)
    extract_text = "\n"
    for p in p_list:
        extract_text=extract_text+"\n"+p.string
        print(p)
        print(p.string)
    return extract_text


rec_res = os.walk('/Users/feidao/Documents/me-book/中医药十三五教材/xue/OEBPS')
filename_list = []
for path, dir_list, file_list in rec_res:
    for file_name in file_list:
        filename_list.append(file_name)
# print(filename_list)
filename_list = filename_list[1:]
filename_list.sort()
# print(filename_list)

BASE_PATH = '/Users/feidao/Documents/me-book/中医药十三五教材/xue/OEBPS'
test = ""
for filename in filename_list:
    if(filename.endswith("html")):
        path = BASE_PATH + '/' + filename
        test += exact_p_tag(path)
    # print(path)
with open("text.txt","w") as file:
    file.write(test)

with open("text.txt","r") as file2:
    list = []
    line = file2.readline()
    json = {
        "name":"",
        "position":"",
        "function":""
    }
    name = ""
    postion = ""
    function = ""
    while line:
        flag = False
        if line.startswith("\n"):
            line = file2.readline()
            continue
        if re.compile("[1-9][0-9]?\..*（[A-Z]{2}.*）.*").match(line):
            name = line
        if line.startswith("【定位】"):
            position = line
        if line.startswith("【主治】"):
            text = line
            while line:
                line = file2.readline()
                flag = True
                match = re.compile("[1-9][0-9]?\..*（[A-Z]{2}.*）.*").match(line)
                print(match)
                if match is None and line is not "\n":
                    text += line
                else:
                    break
            function = text
            list.append({
                    "name":name,
                    "position":position,
                    "function":function
            })
                         
        else:
            if flag==False:
                line = file2.readline()
    print(list)
    print(len(list))
    df = pd.DataFrame(list)
    df["position"] = df["position"].str.replace("【定位】|\n","")
    df["function"] = df["function"].str.replace("【主治】|\n","")
    df.to_excel("text.xlsx", engine="openpyxl", index=False)

    # 手动增加经别


if __name__ == '__main__':
    df2 = pd.read_excel("text2.xlsx", engine="openpyxl")
    #增加content用于穴位索引
    df2["name"] = df2["name"].str.replace("\n","")
    df2["content"] = df2["name"].str.cat([df2["position"],df2["function"],df2["jing"]],sep="|")
    xlsx_path = '/Users/feidao/Documents/me/note/development/vue/vue-learning/yixin-backend/database/basic.xlsx'
    with pd.ExcelWriter(xlsx_path, mode="a", if_sheet_exists="replace") as writer:
        # with pd.ExcelWriter('basic.xlsx') as writer:
        df2.to_excel(writer, sheet_name='xuewei', index=False)



    